import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] dataset = new int[]{10,4,2,7,5,19,11,33,28};
        //System.out.println("优化前：");
        //sort(dataset);
        System.out.println("优化后：");
        sortPro(dataset);
        System.out.println(Arrays.toString(dataset));
    }
    private static void sort(int[] dataset){
        for (int i = 0;i<dataset.length;i++){
            System.out.println("遍历了 "+i+" 轮");
            for (int j = 0;j<dataset.length-i-1;j++){
                if (dataset[j] > dataset[j+1]){
                    int temp = dataset[j];
                    dataset[j] = dataset[j+1];
                    dataset[j+1] = temp;
                }
            }
        }
    }
    // 优化后
    private static void sortPro(int[] dataset){
        for (int i = 0;i<dataset.length;i++){
            System.out.println("遍历了 "+i+" 轮");
            boolean flag = false;
            for (int j = 0;j<dataset.length-i-1;j++){
                if (dataset[j] > dataset[j+1]){
                    int temp = dataset[j];
                    dataset[j] = dataset[j+1];
                    dataset[j+1] = temp;
                    flag = true;
                }
            }
            if (flag == false){
                break;
            }
        }
    }
}
